package com.nanhang.auth.service.impl;
import com.alibaba.fastjson.JSON;
import com.nanhang.auth.entity.UserLogin;
import com.nanhang.constant.TokenConstant;
import com.nanhang.util.JwtUtil;
import com.nanhang.auth.service.Tokenservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class Tokenserviceimpl implements Tokenservice{

    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Map<String, Object> createToken(UserLogin loginUser) {
        String token = loginUser.getToken();
        redisTemplate.opsForValue().set(TokenConstant.REDIS_PREFIX + token, JSON.toJSONString(loginUser));

        //第二件事：将通过jwt加密后的用户信息返回到前端
        //1.1  定义数据map
        Map<String, Object> map = new HashMap<>();
        map.put(TokenConstant.JWT_TOKEN,token);

        //2.2 将上面的数据map进行加密
        String jwtToken = jwtUtil.createToken(map);
        //2.3 定义返回Map
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put(TokenConstant.JWT_ACCESS_TOKEN,jwtToken);

        return resultMap;
    }
}
